﻿#region License, Terms and Author(s)
//
// ELMAH - Error Logging Modules and Handlers for ASP.NET
// Copyright (c) 2004-12 Atif Aziz. All rights reserved.
//
//  Author(s):
//
//      James Driscoll, mailto:jamesdriscoll@btinternet.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion

[assembly: Elmah.Scc("$Id: $")]

namespace Elmah.Analysis
{
    #region Imports
    using System;
    using System.IO;
    using System.Web;
    #endregion

    /// <summary>
    /// Runs the configuration analysis tool
    /// </summary>
    public static class ConfigurationAnalysisHelper
    {
        const string DefaultOutputFilename = ConnectionStringHelper.DataDirectoryMacroString + "elmah_configuration_analysis.txt";

        public static bool RunConfigurationAnalysis()
        {
            return RunConfigurationAnalysis(DefaultOutputFilename);
        }

        public static bool RunConfigurationAnalysis(string outputFilename)
        {
            return RunConfigurationAnalysis(new HttpContextWrapper(HttpContext.Current), outputFilename);
        }

        public static bool RunConfigurationAnalysis(HttpContextBase context)
        {
            return RunConfigurationAnalysis(context, DefaultOutputFilename);
        }

        private static void EnsureDirectory(string path)
        {
            var directory = Path.GetDirectoryName(path);
            Debug.Assert(directory != null);
            if (!Directory.Exists(directory))
                Directory.CreateDirectory(directory);
        }

        public static DateTime LastRunDate { get; private set; }
        public static string LastOutputFilename { get; private set; }

        public static bool RunConfigurationAnalysis(HttpContextBase context, string outputFilename)
        {
            try
            {
                var resolvedOutputFilename = ConnectionStringHelper.ResolveDataSourceFilePath(outputFilename);
                EnsureDirectory(resolvedOutputFilename);

                using (var sw = new StreamWriter(resolvedOutputFilename))
                {
                    var page = new ConfigurationAnalysisOutput{Context = context};
                    sw.Write(page.TransformText());
                }

                LastOutputFilename = outputFilename;
                LastRunDate = DateTime.UtcNow;

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
